﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.iss.inventory.Entity;

namespace Stationery_Inventory.Presentation.PurchaseOrder
{
    public partial class CheckPOHistory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["empID"] == null)
            {
                Response.Redirect("~/Presentation/login.aspx");
            }

            Label lbl = this.Master.FindControl("lblLogInName") as Label;
            if (lbl != null)
            {
                lbl.Text = Session["UserName"].ToString();
            }

            //Populate To Date Textbox Automatically (Today's Date)
            DateTime dateonly = DateTime.Now.Date;
            tbDateTo.Text = dateonly.ToString("dd-MM-yyyy");
        }


        // Populate the Gridview, with Purchase orders in the range of dates selected
        protected void btnCheckPOList_Click(object sender, EventArgs e)
        {
            
            InvManagementEntities invent = new InvManagementEntities();
            DateTime datefrom = Convert.ToDateTime(tbDateFrom.Text);
            DateTime dateto = Convert.ToDateTime(tbDateTo.Text);

            var POHistory = from p in invent.PurchaseOrders
                         where p.DateOrdered >= datefrom && p.DateOrdered <= dateto

                         select new { p.PurchaseID, p.SupplierID, p.DeliveryOrderID, p.OrderedbyID, p.DateOrdered, p.DateRequired, p.Remarks};

            List<object> POList = new List<object>();
            POList = POHistory.ToList<object>();
            gvPurchaseOrdersList.DataSource = POList;
            gvPurchaseOrdersList.DataBind();

            //int purchaseID = Convert.ToInt32(tbPONumber.Text);

            //var supplierName = (from p in invent.PurchaseOrders
            //                    join s in invent.Suppliers on p.SupplierID equals s.SupplierID
            //                    where p.PurchaseID == purchaseID

            //                    select s.Name).First();

            //tbsupplierName.Text = supplierName.ToString();
        }

        //Check stationery item details on each purchase order
        protected void btnCheckPurchaseOrderItems_Click(object sender, EventArgs e)
        {
            InvManagementEntities invent = new InvManagementEntities();
            int purchaseID = Convert.ToInt32(tbPurchaseID.Text);

            var purchaseOrderItems = from p in invent.PurchaseOrderDetails
                                     where p.PurchaseID == purchaseID

                                     select new { p.PurchaseID, p.ItemID, p.Description, p.Qty, p.UnitPrice };
            
            List<object> POItemsList = new List<object>();
            POItemsList = purchaseOrderItems.ToList<object>();
            gvPurchaseOrderItems.DataSource = POItemsList;
            gvPurchaseOrderItems.DataBind();
            
        }
    
    
    
    
    
    }
}